using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace DataAccess
{
	/// <summary>
	/// Strongly-typed collection for the TblFile class.
	/// </summary>
    [Serializable]
	public partial class TblFileCollection : ActiveList<TblFile, TblFileCollection>
	{	   
		public TblFileCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>TblFileCollection</returns>
		public TblFileCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                TblFile o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the TblFiles table.
	/// </summary>
	[Serializable]
	public partial class TblFile : ActiveRecord<TblFile>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public TblFile()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public TblFile(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public TblFile(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public TblFile(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("TblFiles", TableType.Table, DataService.GetInstance("mainConnection"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "ID";
				colvarId.DataType = DbType.Guid;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = false;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarTaskId = new TableSchema.TableColumn(schema);
				colvarTaskId.ColumnName = "TaskId";
				colvarTaskId.DataType = DbType.Guid;
				colvarTaskId.MaxLength = 0;
				colvarTaskId.AutoIncrement = false;
				colvarTaskId.IsNullable = true;
				colvarTaskId.IsPrimaryKey = false;
				colvarTaskId.IsForeignKey = true;
				colvarTaskId.IsReadOnly = false;
				colvarTaskId.DefaultSetting = @"";
				
					colvarTaskId.ForeignKeyTableName = "TblTasks";
				schema.Columns.Add(colvarTaskId);
				
				TableSchema.TableColumn colvarFileName = new TableSchema.TableColumn(schema);
				colvarFileName.ColumnName = "FileName";
				colvarFileName.DataType = DbType.String;
				colvarFileName.MaxLength = -1;
				colvarFileName.AutoIncrement = false;
				colvarFileName.IsNullable = true;
				colvarFileName.IsPrimaryKey = false;
				colvarFileName.IsForeignKey = false;
				colvarFileName.IsReadOnly = false;
				colvarFileName.DefaultSetting = @"";
				colvarFileName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFileName);
				
				TableSchema.TableColumn colvarDateX = new TableSchema.TableColumn(schema);
				colvarDateX.ColumnName = "Date";
				colvarDateX.DataType = DbType.DateTime;
				colvarDateX.MaxLength = 0;
				colvarDateX.AutoIncrement = false;
				colvarDateX.IsNullable = true;
				colvarDateX.IsPrimaryKey = false;
				colvarDateX.IsForeignKey = false;
				colvarDateX.IsReadOnly = false;
				colvarDateX.DefaultSetting = @"";
				colvarDateX.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateX);
				
				TableSchema.TableColumn colvarOriginalFileName = new TableSchema.TableColumn(schema);
				colvarOriginalFileName.ColumnName = "OriginalFileName";
				colvarOriginalFileName.DataType = DbType.String;
				colvarOriginalFileName.MaxLength = -1;
				colvarOriginalFileName.AutoIncrement = false;
				colvarOriginalFileName.IsNullable = true;
				colvarOriginalFileName.IsPrimaryKey = false;
				colvarOriginalFileName.IsForeignKey = false;
				colvarOriginalFileName.IsReadOnly = false;
				colvarOriginalFileName.DefaultSetting = @"";
				colvarOriginalFileName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarOriginalFileName);
				
				TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
				colvarUserId.ColumnName = "UserId";
				colvarUserId.DataType = DbType.Guid;
				colvarUserId.MaxLength = 0;
				colvarUserId.AutoIncrement = false;
				colvarUserId.IsNullable = true;
				colvarUserId.IsPrimaryKey = false;
				colvarUserId.IsForeignKey = true;
				colvarUserId.IsReadOnly = false;
				colvarUserId.DefaultSetting = @"";
				
					colvarUserId.ForeignKeyTableName = "aspnet_Users";
				schema.Columns.Add(colvarUserId);
				
				TableSchema.TableColumn colvarDocId = new TableSchema.TableColumn(schema);
				colvarDocId.ColumnName = "DocId";
				colvarDocId.DataType = DbType.Guid;
				colvarDocId.MaxLength = 0;
				colvarDocId.AutoIncrement = false;
				colvarDocId.IsNullable = true;
				colvarDocId.IsPrimaryKey = false;
				colvarDocId.IsForeignKey = true;
				colvarDocId.IsReadOnly = false;
				colvarDocId.DefaultSetting = @"";
				
					colvarDocId.ForeignKeyTableName = "TblDocuments";
				schema.Columns.Add(colvarDocId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["mainConnection"].AddSchema("TblFiles",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("Id")]
		[Bindable(true)]
		public Guid Id 
		{
			get { return GetColumnValue<Guid>(Columns.Id); }
			set { SetColumnValue(Columns.Id, value); }
		}
		  
		[XmlAttribute("TaskId")]
		[Bindable(true)]
		public Guid? TaskId 
		{
			get { return GetColumnValue<Guid?>(Columns.TaskId); }
			set { SetColumnValue(Columns.TaskId, value); }
		}
		  
		[XmlAttribute("FileName")]
		[Bindable(true)]
		public string FileName 
		{
			get { return GetColumnValue<string>(Columns.FileName); }
			set { SetColumnValue(Columns.FileName, value); }
		}
		  
		[XmlAttribute("DateX")]
		[Bindable(true)]
		public DateTime? DateX 
		{
			get { return GetColumnValue<DateTime?>(Columns.DateX); }
			set { SetColumnValue(Columns.DateX, value); }
		}
		  
		[XmlAttribute("OriginalFileName")]
		[Bindable(true)]
		public string OriginalFileName 
		{
			get { return GetColumnValue<string>(Columns.OriginalFileName); }
			set { SetColumnValue(Columns.OriginalFileName, value); }
		}
		  
		[XmlAttribute("UserId")]
		[Bindable(true)]
		public Guid? UserId 
		{
			get { return GetColumnValue<Guid?>(Columns.UserId); }
			set { SetColumnValue(Columns.UserId, value); }
		}
		  
		[XmlAttribute("DocId")]
		[Bindable(true)]
		public Guid? DocId 
		{
			get { return GetColumnValue<Guid?>(Columns.DocId); }
			set { SetColumnValue(Columns.DocId, value); }
		}
		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a AspnetUser ActiveRecord object related to this TblFile
		/// 
		/// </summary>
		public DataAccess.AspnetUser AspnetUser
		{
			get { return DataAccess.AspnetUser.FetchByID(this.UserId); }
			set { SetColumnValue("UserId", value.UserId); }
		}
		
		
		/// <summary>
		/// Returns a TblDocument ActiveRecord object related to this TblFile
		/// 
		/// </summary>
		public DataAccess.TblDocument TblDocument
		{
			get { return DataAccess.TblDocument.FetchByID(this.DocId); }
			set { SetColumnValue("DocId", value.Id); }
		}
		
		
		/// <summary>
		/// Returns a TblTask ActiveRecord object related to this TblFile
		/// 
		/// </summary>
		public DataAccess.TblTask TblTask
		{
			get { return DataAccess.TblTask.FetchByID(this.TaskId); }
			set { SetColumnValue("TaskId", value.Id); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varId,Guid? varTaskId,string varFileName,DateTime? varDateX,string varOriginalFileName,Guid? varUserId,Guid? varDocId)
		{
			TblFile item = new TblFile();
			
			item.Id = varId;
			
			item.TaskId = varTaskId;
			
			item.FileName = varFileName;
			
			item.DateX = varDateX;
			
			item.OriginalFileName = varOriginalFileName;
			
			item.UserId = varUserId;
			
			item.DocId = varDocId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varId,Guid? varTaskId,string varFileName,DateTime? varDateX,string varOriginalFileName,Guid? varUserId,Guid? varDocId)
		{
			TblFile item = new TblFile();
			
				item.Id = varId;
			
				item.TaskId = varTaskId;
			
				item.FileName = varFileName;
			
				item.DateX = varDateX;
			
				item.OriginalFileName = varOriginalFileName;
			
				item.UserId = varUserId;
			
				item.DocId = varDocId;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn IdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn TaskIdColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn FileNameColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn DateXColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn OriginalFileNameColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn UserIdColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn DocIdColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"ID";
			 public static string TaskId = @"TaskId";
			 public static string FileName = @"FileName";
			 public static string DateX = @"Date";
			 public static string OriginalFileName = @"OriginalFileName";
			 public static string UserId = @"UserId";
			 public static string DocId = @"DocId";
						
		}
		#endregion
		
		#region Update PK Collections
		
        #endregion
    
        #region Deep Save
		
        #endregion
	}
}
